package com.example.lvsuo.modal;

import com.example.lvsuo.entity.SystemUser;
import com.example.lvsuo.mapper.SystemUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class MyUserDetailService implements UserDetailsService {
    @Autowired
    SystemUserMapper us;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SystemUser systemUser = us.queryUserByUsername(username);
        if(systemUser==null){
            throw new UsernameNotFoundException("用户不存在!");
        }
        List<GrantedAuthority> admin = AuthorityUtils.createAuthorityList("admin");
        return new User(systemUser.getUserName(),systemUser.getUserPwd(),admin);
    }
}
